<?php
// 本类由系统自动生成，仅供测试用途
class UsersAction extends AdminPublicAction {
	
	public function _empty(){
		
			$this->redirect('group');	
		}
/*======================================================================
	用戶列表
======================================================================================*/	
    public function user(){
		$gid = I('gid','');
		if(tep_not_null($gid)){
			$list = D('UsersView')->getAll(array('group_id' => $gid));
		}else{
			$list = M('admin')->select();
			}
		
		$this->listing  = $list ;
		$this->left_users = 'current';
		if (S("success") != "")
		{
			$this->assign("msg", '<div class="success">'.S("success").'</div>');
			S('success',NULL);
		}
			
		if (S("err") != "")
		{
			$this->assign("msg", '<div class="err">'.S("err").'</div>');
			S('err',NULL);
		}	
		$this->display();
		
    }
	public function userEdit(){
		$uid = I('id','');
		if(tep_not_null($uid) && is_numeric($uid)){
			$userInfo = M('admin')->where(array('uid' => $uid))->find();
			$this->userInfo  = $userInfo ;
			$this->gid = get_user_group($uid,'id');
			$this->id = $uid;
		}
		if (S("success") != "")
		{
			$this->assign("msg", '<div class="success">'.S("success").'</div>');
			S('success',NULL);
		}
			
		if (S("err") != "")
		{
			$this->assign("msg", '<div class="err">'.S("err").'</div>');
			S('err',NULL);
		}	
		
		if($_SESSION['loginGroupId'] == 1){
			$group = M('think_auth_group')->select();
			$this->group  = $group ;
		}else{
			$group = M('think_auth_group')->where(array('id'=>I('gid')))->find();
			$this->group  = $group ;
		}
		
		
		
		$this->display();
		
    }
	public function userEditHandle(){
		$uid = I('post.id','');
		if(IS_POST){
			
			$data['UserName'] = I('post.username');
			if(tep_not_null($uid) && is_numeric($uid)){
				$where['uid'] = array('neq',$uid);
				$where['UserName'] = array('eq',$data['UserName']);
				$adminCount = M('admin')->where($where)->count();
				if($adminCount == 0){
					$data['uid'] = $uid;
					M('admin')->save($data);
					$map['group_id'] = I('post.group_id');
					M('think_auth_group_access')->where(array('uid' => $uid))->save($map);
					S('success','User successfully updated!');
					$this->redirect('user',array('gid' => I('group_id')));
				}else{
					S('err','UserName already exists!');
					$this->redirect('userEdit',array('id' => $uid));
					}
				
			}else{
				$data['UserPwd'] = md5(I('post.password'));
				$comfrim_password = md5(I('post.comfirm_password'));
				$where['UserName'] = array('eq',$data['UserName']);
				$adminCount = M('admin')->where($where)->count();
				if(I('post.password') == '' || $data['UserName'] == '' ){
					S('err','Please fill in the username and password!');
					$this->redirect('userEdit');
				}
				elseif($adminCount > 0){
					S('err','UserName already exists!');
					$this->redirect('userEdit');
				}
				elseif($data['UserPwd'] != $comfrim_password){
					S('err','Password don\'t match!');
					$this->redirect('userEdit');			
				}else{
					$data['status'] = 1;
					$insterId = M('admin')->add($data);
					$map['uid'] = $insterId;
					$map['group_id'] = I('post.group_id');
					M('think_auth_group_access')->add($map);
					S('success','User successfully added!');
					$this->redirect('user',array('gid' => I('group_id')));	
				}
			}
		}
		
    }
	public function userDelete(){
		$uid = I('id','');
		M('admin')->where(array('uid' => $uid))->delete();
		M('think_auth_group_access')->where(array('uid' => $uid))->delete();
		S('success','User successfully deleted!');
		$this->redirect('user',array('gid' => I('gid')));
		
    }
	public function changePwd(){
		$uid = I('id','');
		$gid = I('gid','');
		$this->id = $uid;
		$this->gid = $gid;
		$this->left_users = 'current';
		if (S("success") != "")
		{
			$this->assign("msg", '<div class="success">'.S("success").'</div>');
			S('success',NULL);
		}
			
		if (S("err") != "")
		{
			$this->assign("msg", '<div class="err">'.S("err").'</div>');
			S('err',NULL);
		}	
		$this->display();
    }
	public function changePwdHandle(){
		if(IS_POST){
			$uid = I('post.id');
			$info = M('admin')->where(array('uid' => $uid))->find();
			$old_Pwd = I('post.old_password');
			$new_Pwd = I('post.new_password');
			$comfirm_Pwd = I('post.comfirm_password');
			if(md5($old_Pwd) != $info['UserPwd']){
				 S('err','the old password is error'); 
				 $this->redirect('changePwd','id='.$uid);
			}
			elseif($new_Pwd != $comfirm_Pwd){
				 S('err','the new password don\'t match'); 
				 $this->redirect('changePwd','id='.$uid);
			}else{
				M('admin')->where(array('uid' => $uid))->setField('UserPwd',md5($new_Pwd));
				S('success','Password successfully updated!');
				$this->redirect('user',array('gid' => I('gid')));
			}
		}
	}
	

/*======================================================================
	用戶組
======================================================================================*/
	public function group(){
		$action =  I('get.action','');
		if($action == 'detail')
		{
			$id = I('id',0,intval);
			if($id > 0 && is_numeric($id))
			{
			  $info = M('think_auth_group')->where(array('id' => $id))->find();
			  $this->info = $info;
			  $this->id = $id;
			}
			//當前用戶的權限菜單
			$currentRuleArr  =array();
			$currentRule =   M('think_auth_group')->where(array('id' => $_SESSION['loginGroupId']))->find();
			$currentRuleArr = explode(',',$currentRule['rules']);
			
			//所有菜單
			foreach(C('Nav') as $k => $v){
				foreach($v as $key => $vo){
					if(in_array($vo,$currentRuleArr) || $_SESSION['loginGroupId'] == 1){
						$rule[$k][$key] = $vo; 	
					}
				}	
			};
			
			
			
			$this->rule = $rule;
		}
		else if($action == 'save')
		{
			$ruleArr = I('post.rule');
			$ruleStr = implode(',',$ruleArr);
			$id = I('post.id');
			if(tep_not_null($id) && is_numeric($id)) 
			{
				$data['title'] = I('post.title');
				$data['rules'] = $ruleStr;   
				M('think_auth_group')->where(array('id' => $id) )->save($data);
				S('success','Group successfully updated!');			
				$this->redirect('group');	
			}else{
				$data['title'] = I('post.title');
				$data['rules'] = $ruleStr;   
				if(M('think_auth_group')->add($data))
				{
					S('success','Group successfully added!');			
					$this->redirect('group');	
				}
			}
		}
		else
		{
			$list = M(' think_auth_group')->select();
			$this->listing  = $list ;
		}
		
		if (S("success") != "")
			{
				$this->assign("msg", '<div class="success">'.S("success").'</div>');
				S('success',NULL);
			}
			
		if (S("err") != "")
		{
				$this->assign("msg", '<div class="err">'.S("err").'</div>');
				S('err',NULL);
		}	
		
		
		
		$this->action = $action;
		$this->left_group = 'current';
		$this->display();
	}
/*======================================================================
	修改顯示
======================================================================================*/
	public function changeStatus(){
			if(session('adminId') == (int)$_GET['id']){		
				echo -1;
			}else{			
				$check = M("admin")->where(array("uid" => (int)$_GET['id']))->getField('status');	
				$user_status = ($check == 1)?0:1;		
				M("admin")->where(array("uid" => (int)$_GET['id']))->setField('status',$user_status);
				echo (int)$user_status;	
			}
			
	}
	
	public function changeGroupStatus(){
			if(session('adminId') == (int)$_GET['id']){		
				echo -1;
			}else{	
				$check = M("think_auth_group")->where(array("id" => (int)$_GET['id']))->getField('status');	
				$user_status = ($check == 1)?0:1;		
				M("think_auth_group")->where(array("id" => (int)$_GET['id']))->setField('status',$user_status);
				echo (int)$user_status;			
			}
	}
}